package co.za.itweb.itwebpro;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



	
	public class mAdapter extends BaseAdapter {
		private Activity activity2;
		String[] itemID;
		String[] titles;
		String[]images;
		String[] summary;
		private LayoutInflater inflater;
		//ImageDownloader imageManager = new ImageDownloader();
		@Override
		public int getCount() {
			return itemID.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		

		public mAdapter(Activity act,String[] item,String[] tit, String[] img,String[] sum) {
		titles=tit;
		images=img;
		summary=sum;
		itemID=item;
		activity2=act;
		inflater = (LayoutInflater) activity2
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
		
		private static class ViewHolder2 {
			public ImageView iconView;
			public TextView titleTextView;
			public TextView bottomText;
			public  ImageLoader imageLoader;
			public LazyLoadImage icondownloader;
			
		}


		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder2 holder;
			
			
        // if (convertView == null) {
        	  convertView = (ViewGroup) inflater.inflate(R.layout.imageslist, null);
				holder = new ViewHolder2();
          

				holder.titleTextView = (TextView) convertView
						.findViewById(R.id.TextOnImage);
				holder.bottomText = (TextView) convertView
					.findViewById(R.id.TextTitle);
				holder.iconView = (ImageView) convertView
						.findViewById(R.id.oneimage);
				holder.imageLoader=new ImageLoader(activity2);
				holder.icondownloader=new LazyLoadImage(holder.iconView);
				convertView.setTag(holder);
				
			//	}else {
				
					holder = (ViewHolder2) convertView.getTag();
				
					  
			//	}
          
         
				
					System.out.println(images[position]+"<<<<<<<<<<");
					if(images[position].length()>4){
						
						
					Bitmap avatar =
							holder.icondownloader.getBitmapFromCache(MainActivity.cachedir+"/"+images[position]);
					
					 if (avatar == null) {
						 holder.icondownloader.execute(MainActivity.cachedir+"/"+images[position]);
					
					} else {
						
						holder.iconView.setImageBitmap(avatar);
					}
					 if( titles[position].length()>90 ){
							String it= titles[position].substring(0,90);
								it=it+"...";
								holder.titleTextView.setText(it);
								}else{
									holder.titleTextView.setText(titles[position]);
								}
					
					 holder.bottomText.setVisibility(View.GONE);
					}else{
					 holder.iconView.setBackgroundColor(Color.parseColor("#ff050505"));
					 if( summary[position].length()>90 ){
						String it= summary[position].substring(0,90);
							it=it+"...";
							holder.titleTextView.setText(it);
							}else{
								holder.titleTextView.setText(summary[position]);
							}
		
							
							holder.titleTextView.setHeight(250);
							
							holder.bottomText.setText(titles[position]);
							holder.bottomText.setTypeface(null, Typeface.BOLD);	
					}

			return convertView;

		}

		

	}



